/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Identificationelement;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Identificationqualifier;
import entityBeans.Typestatus;
import java.util.ArrayList;
import java.util.List;
import entityBeans.Identifiedby;
import entityBeans.Specimen;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class IdentificationelementJpaController {

    public IdentificationelementJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Identificationelement identificationelement) {
        if (identificationelement.getTypestatusList() == null) {
            identificationelement.setTypestatusList(new ArrayList<Typestatus>());
        }
        if (identificationelement.getIdentifiedbyList() == null) {
            identificationelement.setIdentifiedbyList(new ArrayList<Identifiedby>());
        }
        if (identificationelement.getSpecimenList() == null) {
            identificationelement.setSpecimenList(new ArrayList<Specimen>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identificationqualifier identificationqualifier = identificationelement.getIdentificationqualifier();
            if (identificationqualifier != null) {
                identificationqualifier = em.getReference(identificationqualifier.getClass(), identificationqualifier.getIdidentificationqualifier());
                identificationelement.setIdentificationqualifier(identificationqualifier);
            }
            List<Typestatus> attachedTypestatusList = new ArrayList<Typestatus>();
            for (Typestatus typestatusListTypestatusToAttach : identificationelement.getTypestatusList()) {
                typestatusListTypestatusToAttach = em.getReference(typestatusListTypestatusToAttach.getClass(), typestatusListTypestatusToAttach.getIdtypestatus());
                attachedTypestatusList.add(typestatusListTypestatusToAttach);
            }
            identificationelement.setTypestatusList(attachedTypestatusList);
            List<Identifiedby> attachedIdentifiedbyList = new ArrayList<Identifiedby>();
            for (Identifiedby identifiedbyListIdentifiedbyToAttach : identificationelement.getIdentifiedbyList()) {
                identifiedbyListIdentifiedbyToAttach = em.getReference(identifiedbyListIdentifiedbyToAttach.getClass(), identifiedbyListIdentifiedbyToAttach.getIdidentifiedby());
                attachedIdentifiedbyList.add(identifiedbyListIdentifiedbyToAttach);
            }
            identificationelement.setIdentifiedbyList(attachedIdentifiedbyList);
            List<Specimen> attachedSpecimenList = new ArrayList<Specimen>();
            for (Specimen specimenListSpecimenToAttach : identificationelement.getSpecimenList()) {
                specimenListSpecimenToAttach = em.getReference(specimenListSpecimenToAttach.getClass(), specimenListSpecimenToAttach.getIdspecimen());
                attachedSpecimenList.add(specimenListSpecimenToAttach);
            }
            identificationelement.setSpecimenList(attachedSpecimenList);
            em.persist(identificationelement);
            if (identificationqualifier != null) {
                identificationqualifier.getIdentificationelementList().add(identificationelement);
                identificationqualifier = em.merge(identificationqualifier);
            }
            for (Typestatus typestatusListTypestatus : identificationelement.getTypestatusList()) {
                typestatusListTypestatus.getIdentificationelementList().add(identificationelement);
                typestatusListTypestatus = em.merge(typestatusListTypestatus);
            }
            for (Identifiedby identifiedbyListIdentifiedby : identificationelement.getIdentifiedbyList()) {
                identifiedbyListIdentifiedby.getIdentificationelementList().add(identificationelement);
                identifiedbyListIdentifiedby = em.merge(identifiedbyListIdentifiedby);
            }
            for (Specimen specimenListSpecimen : identificationelement.getSpecimenList()) {
                Identificationelement oldIdentificationelementOfSpecimenListSpecimen = specimenListSpecimen.getIdentificationelement();
                specimenListSpecimen.setIdentificationelement(identificationelement);
                specimenListSpecimen = em.merge(specimenListSpecimen);
                if (oldIdentificationelementOfSpecimenListSpecimen != null) {
                    oldIdentificationelementOfSpecimenListSpecimen.getSpecimenList().remove(specimenListSpecimen);
                    oldIdentificationelementOfSpecimenListSpecimen = em.merge(oldIdentificationelementOfSpecimenListSpecimen);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Identificationelement identificationelement) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identificationelement persistentIdentificationelement = em.find(Identificationelement.class, identificationelement.getIdidentificationelement());
            Identificationqualifier identificationqualifierOld = persistentIdentificationelement.getIdentificationqualifier();
            Identificationqualifier identificationqualifierNew = identificationelement.getIdentificationqualifier();
            List<Typestatus> typestatusListOld = persistentIdentificationelement.getTypestatusList();
            List<Typestatus> typestatusListNew = identificationelement.getTypestatusList();
            List<Identifiedby> identifiedbyListOld = persistentIdentificationelement.getIdentifiedbyList();
            List<Identifiedby> identifiedbyListNew = identificationelement.getIdentifiedbyList();
            List<Specimen> specimenListOld = new ArrayList<Specimen>(); //persistentIdentificationelement.getSpecimenList();
            List<Specimen> specimenListNew = new ArrayList<Specimen>(); //identificationelement.getSpecimenList();
            if (identificationqualifierNew != null) {
                identificationqualifierNew = em.getReference(identificationqualifierNew.getClass(), identificationqualifierNew.getIdidentificationqualifier());
                identificationelement.setIdentificationqualifier(identificationqualifierNew);
            }
            List<Typestatus> attachedTypestatusListNew = new ArrayList<Typestatus>();
            for (Typestatus typestatusListNewTypestatusToAttach : typestatusListNew) {
                typestatusListNewTypestatusToAttach = em.getReference(typestatusListNewTypestatusToAttach.getClass(), typestatusListNewTypestatusToAttach.getIdtypestatus());
                attachedTypestatusListNew.add(typestatusListNewTypestatusToAttach);
            }
            typestatusListNew = attachedTypestatusListNew;
            identificationelement.setTypestatusList(typestatusListNew);
            List<Identifiedby> attachedIdentifiedbyListNew = new ArrayList<Identifiedby>();
            for (Identifiedby identifiedbyListNewIdentifiedbyToAttach : identifiedbyListNew) {
                identifiedbyListNewIdentifiedbyToAttach = em.getReference(identifiedbyListNewIdentifiedbyToAttach.getClass(), identifiedbyListNewIdentifiedbyToAttach.getIdidentifiedby());
                attachedIdentifiedbyListNew.add(identifiedbyListNewIdentifiedbyToAttach);
            }
            identifiedbyListNew = attachedIdentifiedbyListNew;
            identificationelement.setIdentifiedbyList(identifiedbyListNew);
            List<Specimen> attachedSpecimenListNew = new ArrayList<Specimen>();
            for (Specimen specimenListNewSpecimenToAttach : specimenListNew) {
                specimenListNewSpecimenToAttach = em.getReference(specimenListNewSpecimenToAttach.getClass(), specimenListNewSpecimenToAttach.getIdspecimen());
                attachedSpecimenListNew.add(specimenListNewSpecimenToAttach);
            }
            specimenListNew = attachedSpecimenListNew;
            identificationelement.setSpecimenList(specimenListNew);
            identificationelement = em.merge(identificationelement);
            if (identificationqualifierOld != null && !identificationqualifierOld.equals(identificationqualifierNew)) {
                identificationqualifierOld.getIdentificationelementList().remove(identificationelement);
                identificationqualifierOld = em.merge(identificationqualifierOld);
            }
            if (identificationqualifierNew != null && !identificationqualifierNew.equals(identificationqualifierOld)) {
                identificationqualifierNew.getIdentificationelementList().add(identificationelement);
                identificationqualifierNew = em.merge(identificationqualifierNew);
            }
            for (Typestatus typestatusListOldTypestatus : typestatusListOld) {
                if (!typestatusListNew.contains(typestatusListOldTypestatus)) {
                    typestatusListOldTypestatus.getIdentificationelementList().remove(identificationelement);
                    typestatusListOldTypestatus = em.merge(typestatusListOldTypestatus);
                }
            }
            for (Typestatus typestatusListNewTypestatus : typestatusListNew) {
                if (!typestatusListOld.contains(typestatusListNewTypestatus)) {
                    typestatusListNewTypestatus.getIdentificationelementList().add(identificationelement);
                    typestatusListNewTypestatus = em.merge(typestatusListNewTypestatus);
                }
            }
            for (Identifiedby identifiedbyListOldIdentifiedby : identifiedbyListOld) {
                if (!identifiedbyListNew.contains(identifiedbyListOldIdentifiedby)) {
                    identifiedbyListOldIdentifiedby.getIdentificationelementList().remove(identificationelement);
                    identifiedbyListOldIdentifiedby = em.merge(identifiedbyListOldIdentifiedby);
                }
            }
            for (Identifiedby identifiedbyListNewIdentifiedby : identifiedbyListNew) {
                if (!identifiedbyListOld.contains(identifiedbyListNewIdentifiedby)) {
                    identifiedbyListNewIdentifiedby.getIdentificationelementList().add(identificationelement);
                    identifiedbyListNewIdentifiedby = em.merge(identifiedbyListNewIdentifiedby);
                }
            }
            for (Specimen specimenListOldSpecimen : specimenListOld) {
                if (!specimenListNew.contains(specimenListOldSpecimen)) {
                    specimenListOldSpecimen.setIdentificationelement(null);
                    specimenListOldSpecimen = em.merge(specimenListOldSpecimen);
                }
            }
            for (Specimen specimenListNewSpecimen : specimenListNew) {
                if (!specimenListOld.contains(specimenListNewSpecimen)) {
                    Identificationelement oldIdentificationelementOfSpecimenListNewSpecimen = specimenListNewSpecimen.getIdentificationelement();
                    specimenListNewSpecimen.setIdentificationelement(identificationelement);
                    specimenListNewSpecimen = em.merge(specimenListNewSpecimen);
                    if (oldIdentificationelementOfSpecimenListNewSpecimen != null && !oldIdentificationelementOfSpecimenListNewSpecimen.equals(identificationelement)) {
                        oldIdentificationelementOfSpecimenListNewSpecimen.getSpecimenList().remove(specimenListNewSpecimen);
                        oldIdentificationelementOfSpecimenListNewSpecimen = em.merge(oldIdentificationelementOfSpecimenListNewSpecimen);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = identificationelement.getIdidentificationelement();
                if (findIdentificationelement(id) == null) {
                    throw new NonexistentEntityException("The identificationelement with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Identificationelement identificationelement;
            try {
                identificationelement = em.getReference(Identificationelement.class, id);
                identificationelement.getIdidentificationelement();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The identificationelement with id " + id + " no longer exists.", enfe);
            }
            Identificationqualifier identificationqualifier = identificationelement.getIdentificationqualifier();
            if (identificationqualifier != null) {
                identificationqualifier.getIdentificationelementList().remove(identificationelement);
                identificationqualifier = em.merge(identificationqualifier);
            }
            List<Typestatus> typestatusList = identificationelement.getTypestatusList();
            for (Typestatus typestatusListTypestatus : typestatusList) {
                typestatusListTypestatus.getIdentificationelementList().remove(identificationelement);
                typestatusListTypestatus = em.merge(typestatusListTypestatus);
            }
            List<Identifiedby> identifiedbyList = identificationelement.getIdentifiedbyList();
            for (Identifiedby identifiedbyListIdentifiedby : identifiedbyList) {
                identifiedbyListIdentifiedby.getIdentificationelementList().remove(identificationelement);
                identifiedbyListIdentifiedby = em.merge(identifiedbyListIdentifiedby);
            }
            List<Specimen> specimenList = identificationelement.getSpecimenList();
            for (Specimen specimenListSpecimen : specimenList) {
                specimenListSpecimen.setIdentificationelement(null);
                specimenListSpecimen = em.merge(specimenListSpecimen);
            }
            em.remove(identificationelement);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Identificationelement> findIdentificationelementEntities() {
        return findIdentificationelementEntities(true, -1, -1);
    }

    public List<Identificationelement> findIdentificationelementEntities(int maxResults, int firstResult) {
        return findIdentificationelementEntities(false, maxResults, firstResult);
    }

    private List<Identificationelement> findIdentificationelementEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Identificationelement.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Identificationelement findIdentificationelement(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Identificationelement.class, id);
        } finally {
            em.close();
        }
    }

    public int getIdentificationelementCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Identificationelement> rt = cq.from(Identificationelement.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
